package com.ruoyi.xysl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.xysl.api.domain.XyslMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xysl.service.ISysXyslMenuService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

/**
 * 菜单Controller
 *
 * @author xysl
 * @date 2025-01-11
 */
@RestController
@RequestMapping("/xyslMenu")
public class SysXyslMenuController extends BaseController
{
    @Autowired
    private ISysXyslMenuService xyslMenuService;

    /**
     * 查询菜单列表
     */
    @RequiresPermissions("enroll:menu:list")
    @GetMapping("/list")
    public AjaxResult list(XyslMenu xyslMenu)
    {
        List<XyslMenu> list = xyslMenuService.selectXyslMenuList(xyslMenu);
        return success(list);
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(XyslMenu xyslMenu)
    {
        List<XyslMenu> menus = xyslMenuService.selectXyslMenuList(xyslMenu);
        return success(xyslMenuService.buildMenuTreeSelect(menus));
    }



    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleXyslMenuTreeselect/{roleId}")
    public AjaxResult roleXyslMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<XyslMenu> menus = xyslMenuService.selectXyslMenuList(new XyslMenu());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", xyslMenuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", xyslMenuService.buildMenuTreeSelect(menus));
        return ajax;
    }


    /**
     * 导出菜单列表
     */
    @RequiresPermissions("enroll:menu:export")
    @Log(title = "菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XyslMenu xyslMenu)
    {
        List<XyslMenu> list = xyslMenuService.selectXyslMenuList(xyslMenu);
        ExcelUtil<XyslMenu> util = new ExcelUtil<XyslMenu>(XyslMenu.class);
        util.exportExcel(response, list, "菜单数据");
    }

    /**
     * 获取菜单详细信息
     */
    @RequiresPermissions("enroll:menu:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(xyslMenuService.selectXyslMenuById(id));
    }

    /**
     * 新增菜单
     */
    @RequiresPermissions("enroll:menu:add")
    @Log(title = "菜单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XyslMenu xyslMenu)
    {
        return toAjax(xyslMenuService.insertXyslMenu(xyslMenu));
    }

    /**
     * 修改菜单
     */
    @RequiresPermissions("enroll:menu:edit")
    @Log(title = "菜单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XyslMenu xyslMenu)
    {
        return toAjax(xyslMenuService.updateXyslMenu(xyslMenu));
    }

    /**
     * 删除菜单
     */
    @RequiresPermissions("enroll:menu:remove")
    @Log(title = "菜单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xyslMenuService.deleteXyslMenuByIds(ids));
    }
}
